package com.fs.controller;


import com.fs.entity.Materials;
import com.fs.entity.ResponseData;
import com.fs.service.IMaterialsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.entity.StuCondition;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.entity.MaterialsCondition;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-12
 */
@RestController
@RequestMapping("/materials")
public class MaterialsController {
    @Autowired
    IMaterialsService MaterialsServiceImpl;
    //获取材料详情
    @GetMapping("getMaterialDetail")
    public ResponseData getMaterialDetail(int mid){
        return ResponseData.ok(MaterialsServiceImpl.getMaterialDetail(mid));
    }
    @Value("${upload.path}")
    private String uploadPath;

    //提交材料
    @PostMapping("/handleMaterial")
    public ResponseData handleMaterial(@RequestParam("file") MultipartFile file, Materials material) {
        if (file.isEmpty()) {
            return ResponseData.error("文件为空");
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        if (fileName == null) {
            return ResponseData.error("文件名为空");
        }
        // 设置文件保存路径
        File dest = new File(uploadPath + File.separator + fileName);
        try {
            // 检查目录是否存在，不存在则创建
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            // 保存文件
            file.transferTo(dest);
            // 更新Material对象
            material.setMimage(fileName);
            // 调用服务处理逻辑
            String result = MaterialsServiceImpl.handle(material);
            if (result.equals("操作成功！")) {
                return ResponseData.ok();
            } else {
                return ResponseData.error("提交失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseData.error("文件上传失败");
        }
    }

    //撤回材料
    @DeleteMapping("withdrawMaterial")
    public ResponseData withdrawMaterial(Integer mid){
        boolean result = MaterialsServiceImpl.withdrawMaterial(mid);
        if (result){
            return ResponseData.ok("撤回成功");
        }
        return ResponseData.error("撤回失败");
    }
    @PostMapping("/updateMaterial")
    public ResponseData updateMaterial(Materials materials){
        boolean result = MaterialsServiceImpl.updateMaterial(materials);
        if (result){
            return ResponseData.ok("修改成功");
        }
        return ResponseData.error("修改失败");
    }
    //保存草稿
    @PostMapping("/saveDraft")
    public ResponseData saveDraft(@RequestParam("file") MultipartFile file,Materials materials){
        if (file.isEmpty()) {
            return ResponseData.error("文件为空");
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        if (fileName == null) {
            return ResponseData.error("文件名为空");
        }
        // 设置文件保存路径
        File dest = new File(uploadPath + File.separator + fileName);
        try {
            // 检查目录是否存在，不存在则创建
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            // 保存文件
            file.transferTo(dest);
            // 更新Material对象
            materials.setMimage(fileName);
            // 调用服务处理逻辑
            Boolean result = MaterialsServiceImpl.save(materials);
            if (result) {
                return ResponseData.ok();
            } else {
                return ResponseData.error("提交失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseData.error("文件上传失败");
        }
    }
    //申请人工审核
    @PostMapping("/manualreview")
    public ResponseData manualreview(int mid){
        boolean result = MaterialsServiceImpl.manual(mid);
        if (result){
            return ResponseData.ok("申请成功");
        }
        return ResponseData.error("申请失败");
    }
    @Autowired
    IMaterialsService materialsService;
    @GetMapping("/queryAiMaterialByCondition")
    public ResponseData queryAiMaterialByCondition(MaterialsCondition materialsCondition){
        Page page=materialsService.queryByPageAndCondition(materialsCondition);

        return ResponseData.ok(page);
    }
    @GetMapping("/updateMaterialresult")
    public ResponseData updateMaterialresult(MaterialsCondition materialsCondition){
        Page page=materialsService.selectPepMaterialsPageWithConditions(materialsCondition);
        return ResponseData.ok(page);
    }

    @PutMapping("/updateMaterialresult")
    public ResponseData updateMaterialresult(Materials materials,Integer rid,String detail,Integer statu){
        if(materialsService.updateMaterialresult(materials,rid,detail,statu))
        {
            return ResponseData.ok();
        }
        return ResponseData.error("哈哈黑");
    }



}
